<?php
namespace app\api\controller;

use app\api\model\News;
use app\api\model\Comment as CommentModel;
use think\validate;


class Comment extends Auth
{

    /**
     * 帖子详细—留言列表
     * @author dong
    */
    public function index($news_id='')
    {
        if(empty($news_id) || $news_id == ''){
            return error('请传当前帖子的ID');
        }
        $list = CommentModel::alias('c')
            ->where('c.news_id', $news_id)
            ->join('jk_user_informations m', 'm.user_id = c.user_id', 'LEFT')     //留言人的信息
            ->join('jk_user_informations h', 'h.user_id = c.p_user_id', 'LEFT')     //回复人的信息
            ->join('jk_news_comments cs', 'cs.pid = c.id', 'LEFT')     //回复人的内容
            ->field('c.id, c.text, c.create_at, c.user_id, m.nickname, m.user_id as user_icon_url, h.nickname as h_nickname, cs.text as cs_text')
            ->order('c.create_at DESC')
            ->paginate($this->number);
        // dump(json_decode( json_encode( $list),true));exit;
        $list = formatPage('commentItem', $list);

        return success($list, '数据获取成功');
    }
   

    /**
	 * 帖子详细—留言或回复留言
	 * @author dong
	*/
    public function add()
    {
    	$data = $this->request->param();
        // dump($data);exit;
        $validate = new Validate(
            [
                ['news_id','require','当前帖子ID不能为空'],
                ['text','require','内容不能为空'],
            ]
        );
        if (!$validate->check($data)) {
            return error($validate->getError());
        }

        if(!empty($data['p_user_id']) && $data['p_user_id'] != ''){     //回复留言
            $p_user_id = $data['p_user_id'];    //被回复的id
            $pid = $data['pid'];    //被回复留言的id
        } else{
            $p_user_id = 0;
            $pid = 0;
        }

        $res = CommentModel::create([
            'news_id' => $data['news_id'],
            'user_id' => $this->user->user_id,
            'create_at' => time(),
            'text' => $data['text'],
            'pid' => $pid,
            'p_user_id' => $p_user_id,
        ]);

        News::where('id', $data['news_id'])->setInc('comment_number');  //留言数+1

        if(!$res){
            return error('留言失败');
        }

        return success($res, '留言成功');
    }

    /**
     * 帖子详细—帖子点赞
     * @author dong
    */
    public function zan($news_id='')
    {
        if(empty($news_id) || $news_id == ''){
            return error('请传当前帖子的ID');
        }
        $map = array(
            'news_id' => $news_id,
            'user_id' => $this->user->user_id,
        );
        $info =  \think\Db::table('jk_news_zans')->where($map)->find();
        if($info){
            return error('你已经点过赞');
        }

        $data = array(
            'user_id' => $this->user->user_id,
            'news_id' => $news_id,
            'create_at' => time()
        );
        $res = \think\Db::table('jk_news_zans')->insert($data);  //添加到记录
        News::where('id', $news_id)->setInc('zan');

        if(!$res){
            return error('点赞失败');
        }

        return success($res, '点赞成功');
    }

}
